package com.meilai.project.service.business.customer.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.constant.MaintenanceStaffChangeEnum;
import com.meilai.project.entity.business.customer.CustomerApplyFollowRecord;
import com.meilai.project.entity.business.customer.CustomerInfo;
import com.meilai.project.entity.business.customer.CustomerMaintainUserRecord;
import com.meilai.project.exception.CommonException;
import com.meilai.project.mapper.business.customer.CustomerApplyFollowRecordMapper;
import com.meilai.project.service.business.customer.CustomerApplyFollowRecordService;
import com.meilai.project.service.business.customer.CustomerInfoService;
import com.meilai.project.service.business.customer.CustomerMaintainUserRecordService;
import com.meilai.project.util.ThreadLocalUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2022-09-09
 */
@Service
public class CustomerApplyFollowRecordServiceImpl extends ServiceImpl<CustomerApplyFollowRecordMapper, CustomerApplyFollowRecord> implements CustomerApplyFollowRecordService {

    @Autowired
    private CustomerMaintainUserRecordService customerMaintainUserRecordService;

    @Autowired
    private CustomerInfoService customerInfoService;

    @Override
    public Boolean applyFollow(Long customer_id) {
        Long user_id = ThreadLocalUserUtil.get().getId();
        CustomerApplyFollowRecord customerApplyFollowRecord = new CustomerApplyFollowRecord();
        customerApplyFollowRecord.setCustomer_id(customer_id);
        customerApplyFollowRecord.setApply_user_id(user_id);
        customerApplyFollowRecord.setStatus(1);
        customerApplyFollowRecord.setApply_time(LocalDateTime.now());
        return this.save(customerApplyFollowRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean pass(Long id) {
        CustomerApplyFollowRecord one = this.getById(id);
        if (one == null) {
            throw new CommonException("数据不存在");
        }
        CustomerInfo customerInfo = customerInfoService.getById(one.getCustomer_id());
        Long user_id = ThreadLocalUserUtil.get().getId();
        LocalDateTime now = LocalDateTime.now();
        CustomerMaintainUserRecord customerMaintainUserRecord = new CustomerMaintainUserRecord();
        customerMaintainUserRecord.setCustomer_id(one.getCustomer_id());
        customerMaintainUserRecord.setNew_maintain_user_id(one.getApply_user_id());
        customerMaintainUserRecord.setOld_maintain_user_id(customerInfo.getMaintenance_staff_id());
        customerMaintainUserRecord.setType(MaintenanceStaffChangeEnum.SHENG_QING_GENG_JING_TONG_GUO.getId());
        customerMaintainUserRecord.setUpdate_time(now);
        // 审核通过 修改申请记录数据、增加客户维护人变更记录数据、修改客户跟进人数据和客户跟进人变更记录数据ID
        return this.update(Wrappers.<CustomerApplyFollowRecord>lambdaUpdate().set(CustomerApplyFollowRecord::getStatus, 2).set(CustomerApplyFollowRecord::getCheck_user_id, user_id).set(CustomerApplyFollowRecord::getCheck_time, now).eq(CustomerApplyFollowRecord::getId, id))
                && customerMaintainUserRecordService.save(customerMaintainUserRecord)
                && customerInfoService.update(Wrappers.<CustomerInfo>lambdaUpdate().set(CustomerInfo::getMaintenance_staff_id, one.getApply_user_id()).set(CustomerInfo::getMaintain_user_record_id, customerMaintainUserRecord.getId()).eq(CustomerInfo::getId, one.getCustomer_id()));
    }

    @Override
    public Boolean reject(Long id) {
        CustomerApplyFollowRecord one = this.getById(id);
        if (one == null) {
            throw new CommonException("数据不存在");
        }
        Long user_id = ThreadLocalUserUtil.get().getId();
        LocalDateTime now = LocalDateTime.now();
        return this.update(Wrappers.<CustomerApplyFollowRecord>lambdaUpdate().set(CustomerApplyFollowRecord::getStatus, 3).set(CustomerApplyFollowRecord::getCheck_user_id, user_id).set(CustomerApplyFollowRecord::getCheck_time, now).eq(CustomerApplyFollowRecord::getId, id));
    }
}
